import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_plugin_gdmap/amap_navi/amap_navi_view.dart';
import 'package:flutter_plugin_gdmap/amap_navi/amap_navi_options.dart';
import 'package:flutter_plugin_gdmap/amap_navi/amap_navi_controller.dart';
import 'package:flutter_plugin_gdmap/model/lat_lng.dart';

class AMapNaviPage extends StatefulWidget {
  @override
  _AMapNaviPageState createState() => _AMapNaviPageState();
}

class _AMapNaviPageState extends State<AMapNaviPage> {

  AMapNaviController _controller;

  //实现监听
  Future<dynamic> platformCallHandler(MethodCall call) async {
    switch (call.method) {
      case "close":
        Navigator.pop(context);
        break;
      case "startNavi":
          changeList();
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        resizeToAvoidBottomInset: false,
        body: SafeArea(
          top: false,
          bottom: false,
          child: Container(
            width: double.maxFinite,
            height: double.maxFinite,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[_aMapNaviViewWidget(), ],
            ),
          ),
        ));
  }

  ///导航布局
  Widget _aMapNaviViewWidget() {
    return Expanded(
      child: AMapNaviView(
        onAMapViewCreated: (controller) {
          setState(() {
            _controller = controller;
            _controller.mapNaviChannel.setMethodCallHandler(platformCallHandler); //设置监听

          });
        },
        options: AmapNaviOptions(trafficInfoUpdateEnabled: true),
      ),
    );
  }

  Future<void> changeList() async {
    LatLng latLng1=  new LatLng(latitude: 30.68900214, longitude: 103.90422821);
    LatLng latLng2=  new LatLng(latitude:  30.7453766, longitude: 104.01580811);

    Map<String, Object> map = {"location": json.encode(latLng1),"endLatlng":json.encode(latLng2)};
    await _controller.navigation(map);
  }
}
